/*
 * Copyright (C) J.P. Morrison, Enterprises, Ltd. 2009, 2012 All Rights Reserved. 
 */
package fbp.text;


import fbp.engine.Component;
import fbp.engine.ComponentDescription;
import fbp.engine.InPort;
import fbp.engine.InputPort;
import fbp.engine.OutPort;
import fbp.engine.OutPorts;
import fbp.engine.OutputPort;
import fbp.engine.Packet;


/**
 * Component to copy all incoming packets to OUT and DUPLICATE
 */
@ComponentDescription("Copy all incoming packets to outputs OUT and DUPLICATE")
@InPort(value = "IN", type = String.class)
@OutPorts({ @OutPort("OUT"), @OutPort(value = "DUPLICATE", type = String.class) })
public class DuplicateString extends Component {

  static final String copyright = "At your option, you may copy, "
      + "distribute, or make derivative works under the terms of the Clarified Artistic License, "
      + "based on the Everything Development Company's Artistic License.  A document describing "
      + "this License may be found at http://www.jpaulmorrison.com/fbp/artistic2.htm. "
      + "THERE IS NO WARRANTY; USE THIS PRODUCT AT YOUR OWN RISK.";

  InputPort inport;

  OutputPort outport;

  OutputPort duplicatePort;

  @Override
  protected void execute() {
    Packet p;
    while ((p = inport.receive()) != null) {
      String s = (String) p.getContent();
      String s2 = new String(s);
      Packet duplicate = create(s2);

      outport.send(p);

      duplicatePort.send(duplicate);

    }
  }

  @Override
  protected void openPorts() {
    inport = openInput("IN");

    outport = openOutput("OUT");
    duplicatePort = openOutput("DUPLICATE");
  }
}
